// FFTDisp.h.h

#ifndef _FFTDISP_h
#define _FFTDISP_h

#if defined(ARDUINO) && ARDUINO >= 100
	#include "Arduino.h"
#else
	#include "WProgram.h"
#endif
#include "StarburstCommon.h"
//-- FHT Options
#define LOG_OUT           1       // use the log output function
#define FHT_N             256     // set to 256 point fht
#define OCTAVE            1       // octave output function
//#define OCT_NORM          1       // normalize out the high values of an octave
//#define SAMPLE_RATE       30000
#define SAMPLE_RATE       28160   //(makes a bucket size of 55hz @ 256 samples, which nicely coincides with A-A piano octaves)
//#define SAMPLE_RATE       14400   // matches serial baud rate
#define SAMPLE_COUNT      1       // number of samples per refresh
#define OCTAVES           MIRRORED_LEGS       // the number of octaves to display

#define AVERAGE_COUNT     (SAMPLE_RATE/FHT_N)     // the number of ffts used to determine a local min/max value (1 seconds worth)
#define GLOBAL_COUNT      (AVERAGE_COUNT * 20)   // the number of ffts used to determine a global min/max value (30 seconds worth)
#define MINIMUM_CAL_RANGE 40
#define LINE_IN_PIN 2
static const unsigned int US_BETWEEN_SAMPLES  = (1000000/SAMPLE_RATE); 


//-- Data
extern int                       oct_bias[OCTAVES];      // the bias for an empty signal per octave
extern int                       oct_averages[OCTAVES];  // the average over SAMPLE_COUNT for the current input
extern int                       count;              // the number of ffts since the last averaging operation
extern uint16_t countSinceAverage;
extern float oct_localAvgCalc[OCTAVES];
extern float oct_localAvg[OCTAVES];
extern uint16_t oct_globalMax[OCTAVES];
extern int readOffset;

void initDisp();

void calibrate();
void printCalibration();

// writes the FFT into the leg data. Returns true if any inputs clipped
bool displayFFT(LegData *data);

bool fht();

void clearGlobal();
bool readInput();
void beforeRead();

void afterRead();
void clearAverages();
void averageOutput();

void avgCalib();
#endif

